<script setup>
import { ref } from 'vue'
import api from '@/api'
import CourseList from '@/views/CourseList.vue'
import { useRoute, useRouter } from 'vue-router'

const $router = useRouter()
const $route = useRoute()
const courses = ref([])
const loading = ref(false)
const collection = ref()

const getCourses = async () => {
  loading.value = true
  try {
    courses.value = await api.get(`/collections/${$route.params.id}/courses`)
  } catch (error) {
    console.error(error)
  }
  loading.value = false
}

const getCollection = async () => {
  try {
    collection.value = await api.get(`/collections/${$route.params.id}`)
  } catch (error) {
    console.error(error)
  }
}

const onClickLeft = () => {
  $router.replace('/')
}

getCourses()
getCollection()
</script>

<template>
  <div>
    <van-nav-bar left-text="返回" left-arrow @click-left="onClickLeft">
      <template #title>
        <div class="navbar" v-if="collection">
          <img :src="collection.image" />
          <div>{{ collection.name }}</div>
        </div>
      </template>
    </van-nav-bar>

    <van-loading v-if="loading" class="page-loading" type="spinner" />

    <van-empty description="暂无课程" v-else-if="!courses.length" class="page-empty" />

    <CourseList :courses="courses"></CourseList>
  </div>
</template>

<style>
#app {
  background: #f9f9f9;
}
.navbar {
  display: flex;
  align-items: center;
  img {
    display: block;
    height: 6vw;
    margin-right: 1vw;
  }
}
</style>
